<?php
namespace app\admin\controller;
use think\Controller;
use think\facade\Db;
use think\facade\Session;
use think\facade\View;

use app\common\model\Cates as CategoryModel;
use app\common\model\Module as ModuleModel;

class Cates extends Base
{
    
    //栏目列表
	public function index(){
        //获取栏目列表
        $param = input("param.");
        $search = [];
        if(isset($param['catname']) && $param['catname'] != ''){
            $search[] = ['catname','like','%'.$param['catname'].'%'];
        }

        $CategoryModel = new CategoryModel();
        $map['siteid'] = $this->admin_siteid;
        $treeList = $CategoryModel->treeList($search,false,$map);
        View::assign('treeList',$treeList);

        return View::fetch();
	}
	
	//栏目添加
    public function add(){
        $param = input('param.');
        if($_POST){
            $CategoryModel = new CategoryModel();
            $res = $CategoryModel -> handle($param);
            if(!$res){
                return dyajaxReturn(0,'操作失败');
            }
            return dyajaxReturn(1,'操作成功');
        }else{
            View::assign('title','栏目');

            $id = isset($param['id'])?$param['id']:0;
            $module = isset($param['module'])?$param['module']:'';
            if(!isset($id)){ $id = '';}
            View::assign('parentid',$id);
            View::assign('module',$module);

            //获取栏目列表
            $CategoryModel = new CategoryModel();
            $treeList = $CategoryModel->treeList([]);
            View::assign('treeList',$treeList);

            $sort = $CategoryModel->max("orderid");
            View::assign('sort',$sort+1);

            return View::fetch();
        }
    }
    
    //修改栏目
    public function edit(){
        
        if($_POST){
            $param = input('param.');

            $CategoryModel = new CategoryModel();
            $res = $CategoryModel -> handle($param);
            if(!$res){
                return dyajaxReturn(0,'操作失败');
            }
            return dyajaxReturn(1,'操作成功');
        }else{
            $CategoryModel = new CategoryModel();

            View::assign('title','栏目');
            $id = input('param.id');
            $info = $CategoryModel->where('id',$id)->find();
            View::assign('info',$info);

            //获取栏目列表
            $treeList = $CategoryModel->treeList([]);
            View::assign('treeList',$treeList);

            View::assign('id',$id);
            return View::fetch();
        }
    }
    
    
    //删除栏目
    public function delete(){
        $id = input('post.id');
        //return dyajaxReturn(0,'3232',$id);
        $ch_res = DB::name('category')->where('parentid',$id)->select();
        if(count($ch_res)>0){
            return dyajaxReturn(0,'请先删除其子栏目！');
        }
        $cate_one = DB::name('category')->where('id','=',$id)->find();

        $res = DB::name('category')->where('id',$id)->delete();
        if(!$res){
            return dyajaxReturn(0,'删除失败');
        }
        adminLog($cate_one['catname'].'栏目删除',$id);
        return dyajaxReturn(1,'删除成功');
    }

    /*//群体删除
    public function allDel(){
        $post = input('post.');
        $ids = $post['ids'];
        //return $ids;
        $id_str = '';
        $ids_arr = explode(',',$ids);
        foreach($ids_arr as $v){
            $child_id = getChildId($v);
            $id_str .= $v.',';
            if($child_id){
                $id_str .= $child_id.',';
            }
        }
        $arr = explode(',',rtrim($id_str,','));
        $arr = array_unique($arr);
        $str = implode(",", $arr);

        $res = DB::name('category')->where("id in ($str)")->delete();

        if($res){
            $return = array(
                'status' => 200,
                'msg'   =>'信息已删除'
            );
            echo json_encode($return);
        }
    }*/


    //状态改变
    public function changeStatus(){
        $param = input('param.');
        $table = $param['table'];
        $id_name = $param['id_name'];
        $id = $param['id'];
        $field = $param['field'];
        $checked = $param['checked'];

        if($checked == 'true'){
            $res = DB::name($table) -> where($id_name,'=',$id)->update([$field=>1]);
        }else{
            $res = DB::name($table) -> where($id_name,'=',$id)->update([$field=>2]);
        }
        if(!$res){
            return dyajaxReturn(0,'操作失败');
        }
        $cat_one = DB::name($table) -> where($id_name,'=',$id)->find();

        adminLog($cat_one['catname'].'栏目字段'.$field.'改变状态',$id);
        return dyajaxReturn(1,'操作成功');
    }

    //改变排序
    public function changeSort(){
        $param = input('param.');
        $table = $param['table'];
        $id_name = $param['id_name'];
        $id = $param['id'];
        $field = $param['field'];
        $val = $param['val'];

        $res = DB::name($table) -> where($id_name,'=',$id)->update([$field=>$val]);
        if(!$res){
            return dyajaxReturn(0,'操作失败');
        }
        $cat_one = DB::name($table) -> where($id_name,'=',$id)->find();
        adminLog($cat_one['catname'].'栏目字段'.$field.'改变排序为'.$val,$id);
        return dyajaxReturn(1,'操作成功');
    }
}
